package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RequestMapping("file")
@RestController
public class FileController {

    @RequestMapping("upload")
    public BaseRespVo upload(MultipartFile file,String username) throws IOException {
        //MultipartFile提供了获得文件的参数的方法
        //上传时的文件名，同时的也可以获得文件类型
        String originalFilename = file.getOriginalFilename();
        //正文类型：文件类型
        String contentType = file.getContentType();
        //文件大小
        long size = file.getSize();
        //请求参数名，在这里的值 👉 file
        String name = file.getName();

        //保存起来，以同一个名称进行保存，会导致覆盖
        File destFile = new File("d:/spring/", originalFilename);
        file.transferTo(destFile);

        return BaseRespVo.ok();
    }

    @RequestMapping("list/upload")
    public BaseRespVo listUpload(MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            File destFile = new File("d:/spring/", originalFilename);
            file.transferTo(destFile);
        }
        return BaseRespVo.ok();
    }
}
